package com.study.service.impl;

import com.alibaba.fastjson.JSON;
import com.study.account.AddressInfo;
import com.study.common.TableId;
import com.study.dao.EcommerceAddressDao;
import com.study.entity.EcommerceAddress;
import com.study.filter.AccessContext;
import com.study.service.IAddressService;
import com.study.vo.LoginUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AddressServiceImpl implements IAddressService{

    @Autowired
    private EcommerceAddressDao ecommerceAddressDao;

    /**
     * 存储多个地址信息
     * @param addressInfo
     * @return
     */
    @Override
    public TableId createAddressInfo(AddressInfo addressInfo) {

        //不能直接从参数中获取用户 id信息
        LoginUserInfo loginUserInfo = AccessContext.getLoginUserInfo();

        //将传递的参数转换成实体对象
        List<EcommerceAddress> ecommerceAddressList = addressInfo.getAddressItems().stream()
                .map(a -> EcommerceAddress.to(loginUserInfo.getId(), a))
                .collect(Collectors.toList());

        //保存到数据表并把返回记录的id 给调用方
        List<EcommerceAddress> saveRecords = ecommerceAddressDao.saveAll(ecommerceAddressList);
        List<Long> ids  =  saveRecords.stream()
                .map(EcommerceAddress::getId)
                .collect(Collectors.toList());

        return new TableId(ids.stream().map(TableId.Id::new).collect(Collectors.toList()));
    }

    @Override
    public AddressInfo getCurrentAddressInfo() {

        LoginUserInfo loginUserInfo = AccessContext.getLoginUserInfo();

        //根据userId 查询到用户的地址信息、再实现转换
        List<EcommerceAddress> ecommerceAddresses = ecommerceAddressDao.findAllByUserId(loginUserInfo.getId());

        List<AddressInfo.AddressItem> addressItems = ecommerceAddresses.stream()
                .map(EcommerceAddress::toAddressItem)
                .collect(Collectors.toList());
        return new AddressInfo(loginUserInfo.getId(),addressItems);
    }

    @Override
    public AddressInfo getAddressInfoById(Long id) {

        EcommerceAddress ecommerceAddress = ecommerceAddressDao.findById(id).orElse(null);

        if(null == ecommerceAddress){
            throw new RuntimeException("Address is not exist");
        }

        return new AddressInfo(ecommerceAddress.getUserId(),
                Collections.singletonList(ecommerceAddress.toAddressItem()));
    }

    @Override
    public AddressInfo getAddressInfoByTableId(TableId tableId) {

        List<Long> ids = tableId.getIds().stream()
                .map(TableId.Id::getId).collect(Collectors.toList());

        log.info("get address info by table id: [{}]", JSON.toJSONString(ids));

        List<EcommerceAddress> ecommerceAddresses =  ecommerceAddressDao.findAllById(ids);

        if(CollectionUtils.isEmpty(ecommerceAddresses)){
            return new AddressInfo(-1L,Collections.emptyList());
        }

        List<AddressInfo.AddressItem> addressItems = ecommerceAddresses.stream()
                .map(EcommerceAddress::toAddressItem)
                .collect(Collectors.toList());

        return new AddressInfo(
                ecommerceAddresses.get(0).getUserId(),addressItems
        );
    }
}
